using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Security.Policy;
using System.Diagnostics;

namespace SearchByGoogle
{
    public partial class Form1 : Form
    {
        string _folder;

        public Form1()
        {
            InitializeComponent();
        }
        
        public Form1(string[] args)
        {
            InitializeComponent();

            if (args.Length > 0)
                _folder=args[0];

            tbFolder.Text = _folder;
        }

        private void btSearch_Click(object sender, EventArgs e)
        {
            Search(tbCriteria.Text, tbFolder.Text);
            Close();
        }

        private void Search(string creteria, string folder)
        {
            string[] words = creteria.Split(' ');
            string strQ = string.Empty;

            foreach (string word in words)
                strQ += string.Format("{0}+", word);
            if (strQ.Length > 1)
                strQ = strQ.Substring(0, strQ.Length - 1);

            if (!string.IsNullOrEmpty(folder))
                strQ += string.Format("+under:\"{0}\"", folder);

            string url = "http://127.0.0.1:4664/search&s=zesl28LDuxYlUi8Reg-XSkd16Ow?q=" + strQ;

            Process.Start(url);
        }

        private void btSelectFolder_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                tbFolder.Text = folderBrowserDialog1.SelectedPath;
        }

        private void tbCriteria_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                Search(tbCriteria.Text, tbFolder.Text);
                Close();
            }
        }
    }
}